<?php
/**
 * Created by PhpStorm.
 * User: a4041
 * Date: 2018/6/13
 * Time: 13:44
 */

namespace App\Http\Controllers\Web;

use App\Http\Controllers\Controller;
use App\Repositories\AddressRepository;
use Illuminate\Http\Request;
use App\Http\Requests\AddressRequest;
use Illuminate\Support\Facades\Validator;


Class AddressController extends Controller
{
    protected $addressRepository;

    public function __construct(AddressRepository $addressRepository)
    {
        //$this->middleware('auth');
        $this->addressRepository = $addressRepository;
    }

    //我的地址
    public function getAllAddress(Request $request)
    {
        return $this->addressRepository->getAllAddress($request);
    }

    //详细地址
    public function getOneAddress(Request $request)
    {
        return $this->addressRepository->getOneAddress($request);
    }

    //添加地址
    public function addAddress(Request $request)
    {
        $rules = [
            'name'=>'required',
            'mobile'=>'required',
            'address' => 'required',
            'address_detail'=>'required',
            'token'=>'string'
        ];
        $message = [
            'address.*'=>'请选择省市区',
            'mobile.*'=>'请填写合法的手机号码',
            'name.*'=>'请填写名称',
            'address_detail.*'=>'请填写详细地址',
            'token.*'=>'请提交令牌'
        ];
        $validator = Validator::make($request->all(), $rules,$message);
        if ($validator->fails()) {
            return show(202,$validator->errors()->first());
        }
        return $this->addressRepository->addAddress($request);
    }

    /*//修改地址
    public function editAddress(Request $request)
    {
        return $this->addressRepository->editAddress($request);
    }*/

    //设置默认地址
    public function defaultAddress(Request $request)
    {
//        $rules = [
//            'id' => 'required|integer|exists:addresses,id',
//        ];
//        $message = [
//            'id'=>'请选择合法的地址'
//        ];
//        $validator = Validator::make($request->all(), $rules,$message);
//        if ($validator->fails()) {
//            return show(202,$validator->errors()->first());
//        }
        return $this->addressRepository->defaultAddress($request);
    }

    //删除地址
    public function deleteAddress(Request $request)
    {
        return $this->addressRepository->deleteAddresses($request);
    }




}